package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.ICarPackageAuditService;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.base.query.QueryObject;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;

@Service
public class ServiceItemServiceImpl implements IServiceItemService {
    @Resource
    private ServiceItemMapper serviceItemMapper;
    @Resource
    private ICarPackageAuditService carPackageAuditService;
    @Resource
    private IBpmnInfoService bpmnInfoService;
    @Resource
    private RuntimeService runtimeService;

    @Override
    public ServiceItem queryById(Long id) {
        return serviceItemMapper.queryById(id);
    }

    @Override
    public TablePageInfo<ServiceItem> query(QueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<>(serviceItemMapper.queryForList(qo));
    }

    @Override
    @Transactional
    public void save(ServiceItem serviceItem) {
        // TODO 校验
        serviceItem.setSaleStatus(ServiceItem.SALESTATUS_OFF);
        if (serviceItem.getCarPackage()) {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        } else {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_NO_REQUIRED);
        }
        serviceItemMapper.insert(serviceItem);
    }


    @Override
    @Transactional
    public void update(ServiceItem serviceItem) {
        // TODO 校验
        ServiceItem oldObj = queryById(serviceItem.getId());
        //处于上架状态的商品不能修改
        if (ServiceItem.SALESTATUS_ON.equals(oldObj.getSaleStatus())) {
            throw new BusinessException("上架服务项目不能修改，请下架后再修改");
        }
        serviceItem.setAuditStatus(oldObj.getAuditStatus());
        // 是套餐且处于审核中不能修改
        if (oldObj.getCarPackage() && ServiceItem.AUDITSTATUS_AUDITING.equals(oldObj.getAuditStatus())) {
            throw new BusinessException("套餐服务项目处于审核中不能修改，请审核通过后再修改");
        } else if (ServiceItem.AUDITSTATUS_APPROVED.equals(oldObj.getAuditStatus())) {
            // 状态修改为初始化
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        } else if (oldObj.getCarPackage()) {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }
        serviceItemMapper.update(serviceItem);
    }

    @Override
    @Transactional
    public void deleteById(Long id) {
        serviceItemMapper.deleteById(id);
    }

    @Override
    public boolean checkNameExsit(String name, Long id) {
        return serviceItemMapper.selectByName(name, id) != null;
    }

    @Override
    public void sale(Long id, Integer saleStatus) {
        ServiceItem serviceItem = queryById(id);
        if (saleStatus.equals(serviceItem.getSaleStatus()))
            throw new BusinessException("套餐服务项操作失败");
        if (serviceItem.getCarPackage() && !ServiceItem.AUDITSTATUS_APPROVED.equals(serviceItem.getAuditStatus()))
            throw new BusinessException("套餐服务项需要审核通过之后才能上架");
        serviceItemMapper.changeSaleStatus(id, saleStatus);
    }

    @Override
    public void changeAuditStatus(Long id, Integer auditingStatus) {
        serviceItemMapper.changeAuditStatus(id, auditingStatus);
    }

    @Override
    public void startAudit(Long id, Long bpmnInfoId, Long director, Long finance, String info) {
        ServiceItem serviceItem = serviceItemMapper.queryById(id);
        if (!ServiceItem.AUDITSTATUS_INIT.equals(serviceItem.getAuditStatus())) {
            throw new BusinessException("参数异常");
        }
        this.changeAuditStatus(id, ServiceItem.AUDITSTATUS_AUDITING);
        CarPackageAudit carPackageAudit = new CarPackageAudit() {
            {
                this.setServiceItemId(id);
                this.setServiceItemInfo(serviceItem.getInfo());
                this.setServiceItemPrice(serviceItem.getDiscountPrice());
                this.setCreator(ShiroUtils.getUser().getUserName());
                this.setBpmnInfoId(bpmnInfoId);
                this.setInfo(info);
                this.setAuditorId(director);
            }
        };
        carPackageAuditService.save(carPackageAudit);
        BpmnInfo bpmnInfo = bpmnInfoService.get(bpmnInfoId);
        // 流程启动, 以金额作为实例参数
        HashMap<String, Object> map = new HashMap<>();
        map.put("director", director);
        if (finance != null) {
            map.put("finance", finance);
        }
        map.put("discountPrice", serviceItem.getDiscountPrice().longValue());
        //启动流程
        ProcessInstance processInstance = runtimeService
                .startProcessInstanceByKey(bpmnInfo.getActProcessKey(), carPackageAudit.getId().toString(), map);
        carPackageAudit.setInstanceId(processInstance.getId());
        carPackageAuditService.update(carPackageAudit);
    }
}
